package com.tiger.jms.sample3;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ReceiverBean implements MessageDrivenBean, MessageListener {
	private transient MessageDrivenContext messageDrivenContext = null;

	public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) {
		this.messageDrivenContext = messageDrivenContext;
	}

	public void ejbCreate() {
	}

	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				System.out.println("[ReceiverBean] Message" + textMessage.getText());
			} else {
				throw new JMSException("error message type.");
			}
		} catch (JMSException e) {
			e.printStackTrace();
			messageDrivenContext.setRollbackOnly();
			throw new EJBException(e);
		}
	}

	public void ejbRemove() {
	}
}
